<?php
if (!defined('IN_IPB')) exit("<h1>Incorrect access</h1> You may not access this file without the acp.");

class lottery_exact extends lottery_template
{
	var $name = "Exact Lottery";
	var $description = "Exact Lottery means that if the ticket numbers are 3,5,6,8,10, with exact order the ticket has to be 3,5,6,8,10 and if having all the number the ticket could be 10,3,6,5,8. A ticket can have repeating numbers";
	
	var $settings = array();
	
	function _load(&$lottery)
	{
		parent::_load($lottery);
		
		if($lottery['l_settings'] || $lottery['l_id'] != 0)
		{
			$this->settings = unserialize($lottery['l_settings']);
		}
		else
		{
			$this->settings['balls'] = 3;
			$this->settings['max_number'] = 10;
		}
	}
	
	function admin_form()
	{
		if($this->ipsclass->input['l_id'])
		{
			return "";
		}
		
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "40%" );
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "60%" );
		
		$html .= $this->ipsclass->adskin->start_table("Edit Extra Lottery settings");
		
		$html .= $this->ipsclass->adskin->add_td_row( array("<b>Number of numbers to pick for this lottery</b>",
															  $this->ipsclass->adskin->form_simple_input("balls",$this->settings['balls'])." Numbers"
													 )      );
		
		$html .= $this->ipsclass->adskin->add_td_row( array("<b>The largest number that can be picked</b>",
															  $this->ipsclass->adskin->form_simple_input("max_number",$this->settings['max_number'])
													 )      );
		
		$html .= $this->ipsclass->adskin->end_table();
		
		return $html;
	}
	
	function admin_save($action,&$lottery)
	{
		if($this->ipsclass->input['l_id'])
		{
			return;
		}
		$this->ipsclass->input['balls'] = intval($this->ipsclass->input['balls']);
		$this->ipsclass->input['max_number'] = intval($this->ipsclass->input['max_number']);
		if($this->ipsclass->input['balls'] < 2)
		{
			$this->ipsclass->admin->error("You need to have at least 2 numbers");
		}
		if($action == 'hidden')
		{
			$lottery['balls'] = $this->ipsclass->input['balls'];
			$lottery['max_number'] = $this->ipsclass->input['max_number'];
		}
		else
		{
			$lottery['l_settings'] = serialize(array(
														'balls' => $this->ipsclass->input['balls'],
														'max_number' => $this->ipsclass->input['max_number'],
											)		);
		}
	}
	
	function prizes_types($editing = 0)
	{
		if(!$this->prizes)
		{
			$use = $editing && $this->ipsclass->input['l_id'] == 0 ? $this->ipsclass->input : $this->settings;
			$this->prizes = array(
									'exact'  => array(
														'display' => "Exact",
														'matching' => 'numbers in exact order',
														'chances' => '1:'.$this->ipsclass->numbers->power($use['max_number'],$use['balls']),
													),
									'having' => array(
														'display' => "Box",
														'matching' => 'numbers not in exact order but have all the numbers',
														'chances' => '1:'.$this->ipsclass->numbers->round($this->ipsclass->numbers->divide($this->ipsclass->numbers->power($use['max_number'],$use['balls']),$this->ipsclass->numbers->factorial($use['balls'])),2),
													),
								);
		}
		return $this->prizes;
	}
	
	function create_numbers()
	{
		$array = array();
		for($i = 1; $i <= $this->settings['max_number']; $i++)
		{
			$array[$i] = $this->settings['max_number'] == 10? $i - 1 : $i;
		}
		
		$rtn = array();
		for($i = 0; $i < $this->settings['balls']; $i++)
		{
			$rtn[$i] = array('max_pick' => 1, 'numbers' => $array);
		}
		return $rtn;
	}
	
	function display_numbers($numbers = array())
	{
		$rtn = "";
		if($this->settings['balls'] <= 10)
		{
			foreach($numbers as $num)
			{
				$rtn .= current($num);
			}
		}
		else
		{
			foreach($numbers as $num)
			{
				$rtn .= current($num).", ";
			}
			$rtn = $this->ipsclass->trim_trailing_comma(trim($rtn));
		}
		return $rtn;
	}
	
	function check_ticket($ticket, $correct_numbers)
	{
		$numbers = unserialize($ticket['numbers_chosen']);
		if($numbers == $correct_numbers)
		{
			return 'exact';
		}
		$nums = array();
		$correct = array();
		foreach($numbers as $num)
		{
			$nums[key($num)]++;
		}
		foreach($correct_numbers as $num)
		{
			$correct[key($num)]++;
		}
		foreach($num as $n => $count)
		{
			if($correct[$n] != $count)
			{
				return false;
			}
			unset($correct[$n]);
		}
		if(count($correct))
		{
			return false;
		}
		return 'having';
	}
}
?>